package com.wyp168.leetcode;

import java.util.HashSet;
import java.util.Set;

/**
 * @ClassName LengthOfLongestSubstring
 * @Description TODO
 * @Author wyp168
 * @Date 2022/7/7 07:56
 */
public class LengthOfLongestSubstring {
    public static void main(String[] args) {
        System.out.println(lengthOfLongestSubstring("pwwkew"));
//        Set<String> set = new HashSet<>();
//        System.out.println(set.add(" "));
    }


    /**
     * @description: 计算最大无重复子串的长度
     * @param: null
     * @return:
     * @author wyp168
     * @date: 2022/7/7 07:57
     */

    public static int lengthOfLongestSubstring(String s) {
        char[] sc = s.toCharArray();
        int length = sc.length;
        int index = 0;
        int count = 0;

        Set<Character> set = new HashSet<>();

        while (index < length) {
            int temp = index;
            int tempCount = 0;
            while (temp < length) {
                if (set.add(sc[temp])) {
                    tempCount ++;
                    temp ++;
                } else {
                    set.removeAll(set);
                    break;
                }
            }
            if (tempCount > count) {
                count = tempCount;
            }
            index ++;
        }
        return count;
    }
}
